<template>
  <div id="app">
    <nav>
      <router-link to="/">Home</router-link> |
      <router-link v-if="!isLoggedIn" to="/login">登录</router-link>
      <template v-if="isLoggedIn">
        <router-link to="/public-activities">公开活动</router-link> |
        <router-link v-if="isAdmin" to="/activity-management">活动管理</router-link> |
        <a href="#" @click.prevent="logout">退出</a>
      </template>
    </nav>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  computed: {
    isLoggedIn() {
      return localStorage.getItem('user') !== null;
    },
    isAdmin() {
      const user = this.$store.state.user || JSON.parse(localStorage.getItem('user'));
      return user && user.role === 'admin';
    }
  },
  methods: {
    logout() {
      // 清除用户信息
      this.$store.commit('setUser', null); // Vuex方式
      // 或者 localStorage.removeItem('user');
      
      // 重置路由
      //this.$router.push('/login');
      
      // 如果你使用了动态路由，可能需要调用重置路由的方法
       this.$router.addRoutesByRole(null);
    }
  }
}
</script>

<style>
/* 保持原有样式不变 */
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
  margin: 0 5px;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>